<template>
  <div class="register-container">
    <div class="register-card">
      <h2 class="title">重置密码</h2>
      <form class="register-form" @submit.prevent="handleResetPassword">
        <!-- 用户名/邮箱（根据需求，可改成仅邮箱或其他校验方式） -->
        <div class="form-group">
          <label class="label-text" for="account">用户名/邮箱</label>
          <input
            type="text"
            id="account"
            v-model="resetForm.account"
            placeholder="请输入用户名或注册邮箱"
            class="input-field"
          />
        </div>
        <!-- 新密码 -->
        <div class="form-group">
          <label class="label-text" for="newPassword">新密码</label>
          <input
            type="password"
            id="newPassword"
            v-model="resetForm.newPassword"
            placeholder="请输入新密码"
            class="input-field"
          />
        </div>
        <!-- 确认新密码 -->
        <div class="form-group">
          <label class="label-text" for="confirmPassword">确认新密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="resetForm.confirmPassword"
            placeholder="请再次输入新密码"
            class="input-field"
          />
        </div>
        <!-- 协议/验证码（可选，若需要可添加） -->
        <div class="agree-group" v-if="needCaptcha">
          <input
            type="checkbox"
            id="agree"
            v-model="agreeTerms"
            class="agree-checkbox"
          />
          <label class="agree-text" for="agree">我已阅读并同意重置协议</label>
        </div>
        <!-- 提交按钮 -->
        <button type="submit" class="register-btn" :disabled="!isFormValid">
          提交重置
        </button>
        <!-- 返回登录 -->
        <div class="login-link" @click="handleGoLogin">返回登录</div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ResetPassword',
  data() {
    return {
      resetForm: {
        account: '', // 用户名/邮箱
        newPassword: '',
        confirmPassword: ''
      },
      agreeTerms: false, // 协议勾选（若需要）
      needCaptcha: false // 是否需要验证码/协议，可根据需求开关
    }
  },
  computed: {
    // 表单校验：密码一致、协议勾选（若开启）
    isFormValid() {
      const { newPassword, confirmPassword } = this.resetForm
      if (this.needCaptcha) {
        return (
          newPassword &&
          confirmPassword &&
          newPassword === confirmPassword &&
          this.agreeTerms
        )
      }
      return newPassword && confirmPassword && newPassword === confirmPassword
    }
  },
  methods: {
    // 提交重置密码逻辑（需对接实际接口）
    async handleResetPassword() {
      try {
        // 根据用户名查找用户信息
        const user = await this.$axios.post('http://localhost:8080/user/find_user_by_username', {
          username: this.resetForm.account,
        });

        if (user.status === 200) {
          const userId = user.data.userid;
          const newPassword = this.resetForm.newPassword;

          // 调用修改密码接口
          const response = await this.$axios.post('http://localhost:8080/user/update_password', {
            userId: userId,
            oldPassword: '', // 重置密码时不需要旧密码
            newPassword: newPassword
          });

          if (response.status === 200 && response.data === '密码修改成功') {
            this.$message.success('密码重置成功，请前往登录！');
            this.$router.push('/login');
          } else {
            this.$message.error('重置失败，请稍后重试');
          }
        } else {
          this.$message.error('用户不存在，请检查用户名');
        }
      } catch (error) {
        this.$message.error('网络异常，请检查后重试');
      }
    },
    // 返回登录页
    handleGoLogin() {
      this.$router.push('/login');
    }
  }
}
</script>

<style scoped>
/* 大容器样式（复用注册页） */
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(120deg, #E0F7FA 0%, #B2EBF2 100%); 
}

/* 注册卡片样式（复用注册页） */
.register-card {
  width: 340px;
  padding: 40px;
  border-radius: 24px;
  background: linear-gradient(135deg, #4FC3F7 0%, #81D4FA 100%); 
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); 
  color: #fff;
  text-align: center;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
}

.register-card::before {
  content: "🔒";
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 30px;
}

.register-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15); 
}

/* 标题样式（复用注册页） */
.title {
  margin-bottom: 32px;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase; 
  animation: titleSlide 1s ease-in-out;
}

@keyframes titleSlide {
  from {
    transform: translateY(-20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* 表单组通用样式（复用注册页） */
.form-group {
  margin-bottom: 24px;
  text-align: left;
  animation: formFade 1s ease-in-out;
}

@keyframes formFade {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 标签文本样式（复用注册页） */
.label-text {
  display: block;
  margin-bottom: 8px;
  font-size: 15px;
  color: #fff; 
  font-weight: 500;
}

/* 输入框样式（复用注册页） */
.input-field {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  border: none;
  border-radius: 16px;
  background-color: #E0F7FA; 
  color: #333;
  font-size: 15px;
  transition: box-shadow 0.3s ease, transform 0.3s ease;
}

.input-field:focus {
  outline: none;
  box-shadow: 0 0 12px rgba(79, 195, 247, 0.6); 
  transform: scale(1.02); 
}

/* 协议勾选区域样式（复用注册页，按需显示） */
.agree-group {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 24px;
  animation: formFade 1s ease-in-out;
  animation-delay: 0.2s;
  opacity: 0;
  animation-fill-mode: forwards;
}

.agree-checkbox {
  width: 18px;
  height: 18px;
  margin-right: 10px;
  accent-color: #03A9F4; 
}

.agree-text {
  font-size: 14px;
  color: #fff; 
}

/* 注册按钮样式（复用注册页，改为“提交重置”逻辑） */
.register-btn {
  width: 100%;
  height: 48px;
  border: none;
  border-radius: 16px;
  background: linear-gradient(90deg, #03A9F4 0%, #29B6F6 100%); 
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  animation: formFade 1s ease-in-out;
  animation-delay: 0.4s;
  opacity: 0;
  animation-fill-mode: forwards;
  position: relative;
}

.register-btn::after {
  content: "✅";
  position: absolute;
  right: 15px;
  opacity: 0;
  transition: all 0.3s ease;
}

.register-btn:hover {
  box-shadow: 0 4px 16px rgba(3, 169, 244, 0.4); 
  transform: translateY(-2px);
}

.register-btn:hover::after {
  opacity: 1;
  right: 20px;
}

.register-btn:active {
  transform: scale(0.96);
}

.register-btn:disabled {
  background: #bdc3c7;
  color: #fff;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

/* 登录链接样式（复用注册页，改为“返回登录”） */
.login-link {
  margin-top: 20px;
  font-size: 15px;
  color: #fff; 
  text-decoration: underline;
  cursor: pointer;
  transition: color 0.3s ease, transform 0.3s ease;
  animation: formFade 1s ease-in-out;
  animation-delay: 0.6s;
  opacity: 0;
  animation-fill-mode: forwards;
}

.login-link:hover {
  color: #03A9F4; 
  transform: translateY(-1px);
}
</style>